package com.zed.api.common.vo;


import com.zed.api.common.collection.MemberCollection;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 分页VO对象
 *
 * @author Zed
 */
public class PageVO<E> {

    private List<E> items;

    private long total;

    public PageVO(Builder<E> builder) {
        this.items = builder.items == null ? Collections.emptyList() : Collections.unmodifiableList(builder.items);
        this.total = builder.total;
    }


    /**
     * 条目
     *
     * @return {@link List}
     */
    public List<E> getItems() {
        return this.items;
    }

    /**
     * 总数
     *
     * @return {@link long}
     */
    public long getTotal() {
        return this.total;
    }

    /**
     * 创建构建器
     *
     * @return {@link Builder}
     */
    public static <E> Builder<E> newBuilder() {
        return new Builder<E>();
    }

    public static class Builder<E> {

        private List<E> items;

        private long total;

        /**
         * 设置条目
         *
         * @param items {@link List}
         * @return {@link Builder}
         */
        public Builder<E> items(List<E> items) {
            this.items = items;
            return this;
        }

        /**
         * 设置条目
         *
         * @param collection {@link MemberCollection},一组对象的集合
         * @return {@link Builder}
         */
        public Builder<E> items(MemberCollection<E> collection) {
            this.items = collection.getItems();
            return this;
        }

        /**
         * 添加条目
         *
         * @param item {@link E}
         * @return {@link Builder}
         */
        public Builder<E> addItem(E item) {
            if (this.items == null) {
                this.items = new ArrayList<>();
            }
            this.items.add(item);
            return this;
        }

        /**
         * 移除条目
         *
         * @param item {@link E}
         * @return {@link Builder}
         */
        public Builder<E> removeItem(E item) {
            if (this.items == null) {
                this.items = new ArrayList<>();
            }
            this.items.remove(item);
            return this;
        }


        /**
         * 总数
         *
         * @param total {@link long},总数
         * @return {@link Builder}
         */
        public Builder<E> total(int total) {
            this.total = total;
            return this;
        }

        /**
         * 总数
         *
         * @param total {@link long},总数
         * @return {@link Builder}
         */
        public Builder<E> total(long total) {
            this.total = total;
            return this;
        }

        /**
         * 构建对象
         *
         * @return {@link PageVO}
         */
        public PageVO<E> build() {
            return new PageVO<>(this);
        }

    }
}
